package com.shop.cereshop.app.job;

import com.shop.cereshop.app.dao.buyer.CereBuyerUserDAO;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.calculate.Calculate;
import com.shop.cereshop.commons.domain.user.FriendUserInfoVo;
import com.shop.cereshop.commons.enums.RedisEnum;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Order(9)
@Slf4j
public class InitMemberJob implements CommandLineRunner {
    @Resource
    private CereBuyerUserDAO cereBuyerUserDAO;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {
//        initMember();
    }

    /**
     * 2、处理店铺相关的标签
     */
    @Scheduled(fixedDelay = 1000 * 60 * 5)
    public void initMember() throws Exception {
        redisTemplate.delete(RedisEnum.MEMBER_USER_INFO.getKey());
        // 查询全部会员信息
        List<FriendUserInfoVo> all = cereBuyerUserDAO.selectListByIsMember();
        //按照用户id进行分组 用户id是唯一 所以每组只有一条用户数据 getBuyerUserId 是long 需要转
        Map<String, FriendUserInfoVo> map = all.stream().collect(Collectors.toMap(FriendUserInfoVo::getBuyerUserId, v -> v));
        redisTemplate.opsForHash().putAll(RedisEnum.MEMBER_USER_INFO.getKey(), map);
        log.info("初始化会员信息完成 {}", all.size());
    }
}
